﻿using Common.AspNetCore;
using Common.Infrastructure;
using Common.Infrastructure.Structs;
using Common.Models.FCP;
using Common.RunModels.FCP;
using FCP.Api.Models;
using FCP.Api.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FCP.Api.Controllers
{
    [Route("api/FCP")]
    public class FCPController : ApiController
    {
        private readonly FCPService fpcService;
        private readonly FCPInitService initService;
        public FCPController(IServiceProvider provider, FCPService auth, FCPInitService init)
             : base(provider)
        {
            this.fpcService = auth;
            this.initService = init;
        }

        [HttpPost, Route("GetFormConfigSql")]
        public async Task<ApiActionResult<FCPSqlRespone>> GetFormConfigSql([FromBody] FCPSqlRequest request)
        {
            return await SafeExecuteAsync<FCPSqlRespone>(async (param) =>
            {
                FCPSqlRespone respone = new FCPSqlRespone();
                var sqls = await this.fpcService.GetFormConfigSqlAsync(request);
                respone.Sqls = sqls;
                return respone;
            });
        }

        [HttpPost, Route("GetAllAuthFormByPlatform")]
        public async Task<ApiActionResult<List<FCPFormAuth>>> GetAllAuthFormByPlatform([FromBody] AuthFormByPlatformParam request)
        {
            return await SafeExecuteAsync<List<FCPFormAuth>>(async (param) =>
            {
                return await this.fpcService.GetAllAuthFormByPlatformAsync(request.OrgId, request.PlatformType, request.Lang);
            });
        }

        [HttpPost, Route("GetFormLastVersion")]
        public async Task<ApiActionResult<List<string>>> GetFormLastVersion([FromBody] FCPFormVersionParam request)
        {
            return await SafeExecuteAsync<List<string>>(async (param) =>
            {
                return await this.fpcService.GetFormLastVersionAsync(request.OrgId, request.Time);
            });
        }

        [HttpPost, Route("GetAllFormByIds")]
        public async Task<ApiActionResult<List<FCPForm>>> GetAllFormByIds([FromBody] FCPFormByIdParam request)
        {
            return await SafeExecuteAsync<List<FCPForm>>(async (param) =>
            {
                return await this.fpcService.GetAllFormByIdsAsync(request.Ids, request.Lang);
            });
        }

        [HttpPost, Route("GetEnumInfos")]
        public async Task<ApiActionResult<List<EnumInfo>>> GetEnumInfos([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<EnumInfo>>(async (param) =>
            {
                return await this.fpcService.GetEnumInfosAsync(apiParam);
            });
        }


        [HttpPost, Route("GetEnumByStringInfosByEnumName")]
        public async Task<ApiActionResult<List<EnumByStringInfo>>> GetEnumByStringInfosByEnumName([FromBody] ApiParam apiParam)
        {
            return await SafeExecuteAsync<List<EnumByStringInfo>>(async (param) =>
            {
                return await this.fpcService.GetEnumByStringInfosByEnumNameAsync(apiParam);
            });
        }

        [HttpPost, Route("InitOrg")]
        public async Task<ApiActionResult<FCPInitReponse>> InitOrg([FromBody] FCPInitParam initParam)
        {
            return await SafeExecuteAsync<FCPInitReponse>(async (param) =>
            {
                return await this.initService.InitOrgAsync(initParam);
                
            });
        }

        [HttpPost, Route("SyncFormConfig")]
        public async Task<ApiActionResult<FCPInitReponse>> SyncFormConfig([FromBody] FCPInitByFormParam initParam)
        {
            return await SafeExecuteAsync<FCPInitReponse>(async (param) =>
            {
                return await this.initService.SyncFormConfigAsync(initParam);

            });
        }

        [HttpPost, Route("SyncFormConfigBySelfForm")]
        public async Task<ApiActionResult<FCPInitReponse>> SyncFormConfigBySelfForm([FromBody] FCPInitBySelfFormParam initParam)
        {
            return await SafeExecuteAsync<FCPInitReponse>(async (param) =>
            {
                return await this.initService.SyncFormConfigBySelfFormAsync(initParam);

            });
        }

        [HttpPost, Route("InitSys")]
        public async Task<ApiActionResult<FCPInitReponse>> InitSys([FromBody] FCPInitParam initParam)
        {
            return await SafeExecuteAsync<FCPInitReponse>(async (param) =>
            {
                return await this.initService.InitSysAsync(initParam);
              
            });
        }

        [HttpPost, Route("GetBaseLabelsByIds")]
        public async Task<ApiActionResult<List<m_label>>> GetBaseLabelsByIds([FromBody] ModelIdRequest initParam)
        {
            return await SafeExecuteAsync<List<m_label>>(async (param) =>
            {
                return await this.fpcService.GetBaseLabelsByIdsAsync(initParam);

            });
        }

        [HttpPost, Route("SaveBaseLabel")]
        public async Task<ApiActionResult<bool>> SaveBaseLabel([FromBody] ModelSaveRequest initParam)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await this.fpcService.SaveBaseLabelAsync(initParam);

            });
        }

        [HttpPost, Route("GetBaseLabelOrderPage")]
        public async Task<ApiActionResult<ApiPagedResult<m_label>>> GetBaseLabelOrderPage([FromBody] ModelOrderPageRequest initParam)
        {
            return await SafeExecuteAsync<ApiPagedResult<m_label>>(async (param) =>
            {
                return await this.fpcService.GetBaseLabelOrderPageAsync(initParam);

            });
        }

        [HttpPost, Route("SaveEnumInfoToEnum")]
        public async Task<ApiActionResult<bool>> SaveEnumInfoToEnum([FromBody] FCPEnumInfoParamModel initParam)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await this.fpcService.SaveEnumInfoToEnumAsync(initParam);

            });
        }

        [HttpPost, Route("SaveEnum")]
        public async Task<ApiActionResult<bool>> SaveEnum([FromBody] ModelSaveRequest model)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                return await this.fpcService.SaveEnumAsync(model);

            });
        }

        [HttpPost, Route("FormConfigVersionByFormCodes")]
        public async Task<ApiActionResult<bool>> FormConfigVersionByFormCodes([FromBody] FCPFormVersionResetParam model)
        {
            return await SafeExecuteAsync<bool>(async (param) =>
            {
                await this.fpcService.FormConfigVersionByFormCodesAsync(model);
                return true;

            });
        }
    }
}
